﻿using AutoMapper;
using MongoDB.Bson;
using Project.Core.Api.Controllers.Organization.Dto;
using Project.Core.Api.Controllers.System.IMenu.Dto;
using Project.Core.Api.Controllers.System.IPermission.Dto;
using Project.Core.Api.Controllers.System.IRole.Dto;
using Project.Core.Api.Controllers.System.IUser.Dto;
using Project.Core.Model;
using Project.Core.Model.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Project.Core.Api.AutoMapper
{
    /// <summary>
    /// AutoMapper
    /// </summary>
    public class AutoMapperConfig : Profile
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public AutoMapperConfig()
        {
            // 菜单
            //CreateMap<PermissionAddDto, UserPermission>();

            //CreateMap<UserPermission, PermissionDto>();

            //// 组织机构
            //CreateMap<OrganizationAddDto, UserOrganization>();

            //CreateMap<UserOrganization, OrganizationDto>();

            //// CreateMap<string, int>().ConvertUsing(s => Convert.ToInt32(s));
            //CreateMap<Menu, MenuDto>().ForMember(x => x.Belong, opt => opt.MapFrom(y => y.Belong));

            //
            //CreateMap<MenuDto, Menu>().ForMember(x => x.Belong, opt => opt.MapFrom(y => y.Belong));
            //CreateMap<MenuDto, Menu>().ConvertUsing<TypeTypeConverter>();
            //#region 菜单Dto 转 菜单实体
            //CreateMap<MenuDto, Menu>().ForMember(x => x._id, opt =>
            //{
            //    opt.Condition(y => !string.IsNullOrEmpty(y._id));
            //}).ConvertUsing(s => new Menu
            //{
            //    Belong = !string.IsNullOrEmpty(s.Belong) ? ObjectId.Parse(s.Belong) : new ObjectId(),
            //    CreateTime = s.CreateTime,
            //    DeleteTime = s.DeleteTime,
            //    IsValid = s.IsValid,
            //    MenuIcon = s.MenuIcon,
            //    MenuName = s.MenuName,
            //    MenuPath = s.MenuPath,
            //    Operation = s.Operation,
            //    _id = !string.IsNullOrEmpty(s._id) ? ObjectId.Parse(s._id) : new ObjectId()
            //});
            //#endregion

            #region 角色Dto 转 角色实体
            CreateMap<UserRoleDto, UserRole>().ForMember(x => x.Id, opt =>
            {
                opt.Condition(y => y.Id == 0);
            }).ConvertUsing(s => new UserRole
            {
                Belong = s.Belong,
                CreateTime = s.CreateTime,
                DeleteTime = s.DeleteTime,
                IsValid = s.IsValid,
                Remark = s.Remark,
                RoleName = s.RoleName,
                Creater = s.Creater,
                QueryAuthority = s.QueryAuthority,
                Id = s.Id
            });
            #endregion

            #region 组织机构Dto 转 组织机构实体
            CreateMap<OrganizationDto, UserOrganization>().ConvertUsing(s => new UserOrganization
            {
                Belong = s.Belong,
                CreateTime = s.CreateTime,
                DeleteTime = s.DeleteTime,
                IsValid = s.IsValid,
                Remark = s.Remark,
                OrganizationName = s.OrganizationName,
                Creater = s.Creater,
                Id = s.Id
            });
            #endregion

            //#region 用户菜单权限Dto 转用户菜单权限实体
            //CreateMap<DetailPermissionDto, Permission>().ConvertUsing(s => new Permission
            //{
            //    _id = !string.IsNullOrEmpty(s.Id) ? ObjectId.Parse(s._id) : new ObjectId(),
            //    // Children = s.Children,
            //    MenuIcon = s.MenuIcon,
            //    MenuName = s.MenuName,
            //    Operations = s.Operations,
            //    MenuPath = s.MenuPath
            //});

            //CreateMap<UserPermissionDto, UserPermission>()
            //.ConvertUsing(s => new UserPermission
            //{
            //    RoleID = !string.IsNullOrEmpty(s.RoleID) ? ObjectId.Parse(s.RoleID) : new ObjectId(),
            //    //Children = new List<Permission>() {
            //    //    s.Children
            //    //},
            //    // Children = s.Children.ToMap<Permission>(),
            //    _id = !string.IsNullOrEmpty(s._id) ? ObjectId.Parse(s._id) : new ObjectId(),
            //    CreateTime = s.CreateTime,
            //    IsValid = s.IsValid
            //});

            //CreateMap<UserPermission, UserPermissionDto>()
            //.ConvertUsing(s => new UserPermissionDto
            //{
            //    RoleID = s.RoleID.ToString(),
            //    //Children = new List<Permission>() {
            //    //    s.Children
            //    //},
            //    // Children = s.Children.ToMap<Permission>(),
            //    _id = s._id.ToString(),
            //    CreateTime = s.CreateTime,
            //    IsValid = s.IsValid
            //});
            //#endregion

            #region 用户Dto 转用户实体
            CreateMap<UserDto, User>();
            CreateMap<User, UserDto>();
            #endregion
            //CreateMap<Menu, MenuDto>().ConvertUsing(s => new MenuDto
            //{
            //    Belong = s.Belong.ToString()
            //});
        }
    }

    //public interface ITypeConverter<MenuDto, Menu>
    //{
    //    ObjectId Convert(MenuDto source);
    //}

    //public class TypeTypeConverter : ITypeConverter<MenuDto, Menu>
    //{
    //    public ObjectId Convert(MenuDto source)
    //    {
    //        return ObjectId.Parse(source.Belong);
    //    }
    //}
}
